package cn.tjie.basic.service.impl;

import cn.tjie.basic.mapper.BaseMapper;
import cn.tjie.basic.query.BaseQuery;
import cn.tjie.basic.service.IBaseService;
import cn.tjie.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 业务层的实现类：基础业务实现
 */
//事务注解，查询的事务定义
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired//自动注入
    private BaseMapper<T> baseMapper;

    @Override
    //事务注解，增删改事务定义
    @Transactional
    public void save(T t) {
        baseMapper.save(t);
    }

    @Override
    //事务注解，增删改事务定义
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    //事务注解，增删改事务定义
    @Transactional
    public void del(Long id) {
        baseMapper.del(id);
    }

    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }

    @Override
    public T loadById(Long id) {
        return baseMapper.loadById(id);
    }

    /**
     * 分页和高级查询
     * @param baseQuery
     * @return
     */
    @Override
    public PageList<T> pageQuery(BaseQuery baseQuery) {
        //查询分页总数量
        Integer totals = baseMapper.queryCount(baseQuery);
        //如果总数量等于0，就不需要数据了  用默认的
        if (totals == 0) {
            return new PageList<>();
        }

        //查询分页的数据
        List<T> data = baseMapper.queryData(baseQuery);
        return new PageList<>(data,totals);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void patchDelete(List<Long> ids) {
        baseMapper.patchDelete(ids);
    }
}
